import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;

import org.apache.axis.message.addressing.Address;
import org.apache.axis.message.addressing.EndpointReferenceType;
import org.globus.examples.clients.MathService_instance_rp.Client;
import org.globus.examples.services.core.rp.impl.MathQNames;
import org.globus.examples.stubs.MathService_instance_rp.MathPortType;
import org.globus.examples.stubs.MathService_instance_rp.service.MathServiceAddressingLocator;

import solvers.DisplaySolver;
import utils.SerializeHelper;

public class AppMain

{
	ServicePublisher servicePublisher;
	ServiceFinder serviceFinder;
	ServiceManager serviceManager;

	public static void main1(String[] args) {
			// SerializeHelper.SendObjectToFile(1, "file1.txt");
			// System.out.println(SerializeHelper.GetObjectFromFile("file1.txt"));
		// try
		// {
		// Class c = Class.forName("solvers.DisplaySolver");
		// Class[] types = new Class[]
		// { java.lang.Float.class, java.lang.String.class,
		// java.lang.String.class, java.lang.Float.class };
		// Method m = c.getMethod("run", types);
		// Object[] parameters = new Object[]
		// { 10f, "a", "b", 20f };
		// Object obj = c.newInstance();
		// System.out.println(obj.toString());
		// m.invoke(obj, parameters);
		// System.out.println(obj.toString());
		//
		// }
		// catch (Exception exc)
		// {
		// System.out.println("ERROR:" + exc.getMessage());
		// }
		// Client c = new Client();

		AppMain gsm = new AppMain();
		
//		SerializeHelper.SendObjectToFile("dadasd sdasdas", "file1.txt");
//		System.out.println((SerializeHelper.GetObjectFromFile("file1.txt")));
//		try {
//			FileReader reader = new FileReader("words.txt");
//			BufferedReader bufferedReader = new BufferedReader(reader);
//			String line = bufferedReader.readLine();
//			String[] words = line.split(" ");
//			Client c=new Client();
//			long startTime=System.currentTimeMillis();
//			for (int i = 0; i < words.length; i++) {
//				System.out.println("Executing service:"+i);
//				c.Execute("http://172.16.171.148:8080/wsrf/services/examples/core/rp/MathService", words[i]);
//				System.out.println(c.toString());
//			}
//			long endTime=System.currentTimeMillis();
//			long duration=endTime-startTime;
//			System.out.println(duration+" in milliseconds");
//			System.out.println("To process all "+words.length+" words it took");
//			int minutes=(int)(duration/(1000*60));
//			duration=duration%(1000*60);
//			System.out.println(minutes+" minutes");
//			int seconds=(int)(duration/(1000));
//			duration=duration%(1000);
//			System.out.println(seconds+" seconds");
//			System.out.println(duration+" milliseconds");
//		} catch (Exception exc) {
//			System.out.println("ERROR:" + exc.getMessage());
//		}
		// Client c = new Client();
		// System.out.println("!!!");
		// try
		// {
		// System.out.println(c.toString());
		// c
		// .run("http://172.16.171.148:8080/wsrf/services/examples/core/rp/MathService");
		// System.out.println(c.toString());
		// SerializeHelper.SendObjectToFile(c, "f.txt");
		// System.out.println("-----");
		// System.out.println(SerializeHelper.GetObjectFromFile("f.txt"));
		// }
		// catch (Exception exc)
		// {
		// System.out.println(exc.getMessage());
		// }
	}

	public AppMain() {
		try {
			System.out.println("Choose component to start:");
			System.out.println("1.ServicePublisher");
			System.out.println("2.ServiceManager");
			System.out.println("3.ServiceFinder");

			InputStreamReader reader = new InputStreamReader(System.in);
			BufferedReader bufferedReader = new BufferedReader(reader);
			int i = Integer.parseInt(bufferedReader.readLine());
			switch (i) {
			case 1:
				servicePublisher = new ServicePublisher();
				servicePublisher.Start();
				break;
			case 2:
				serviceManager = new ServiceManager();
				serviceManager.Start();
				break;
			case 3:
				serviceFinder = new ServiceFinder();
				serviceFinder.Start();
				break;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// serviceFinder = new ServiceFinder();
		// serviceFinder.Initialize(ConfigMode.EDGE, "peerName", false);
		// status = ClientStatus.Available;
		// solver = new Solver();

		// AdvertisementFactory.registerAdvertisementInstance(
		// ServiceRequesterAdvertisement.getAdvertisementType(),
		// new ServiceRequesterAdvertisement.Instantiator());
		//
		// ServiceRequesterAdvertisement adv = (ServiceRequesterAdvertisement)
		// AdvertisementFactory
		// .newAdvertisement(ServiceRequesterAdvertisement
		// .getAdvertisementType());
		// adv.setAddParam("param param param");
		// adv.setServiceMethod("class1");
		// adv.setServiceName("serviceName :)");
		// adv.setServiceType(ServiceType.Application);
		// adv.setId(IDFactory.newCodatID(globusServiceFinder.getGroup()
		// .getPeerGroupID()));
		// Document doc = adv.getDocument(MimeMediaType.TEXTUTF8);
		// try
		// {
		// doc.sendToStream(System.out);
		// }
		// catch (IOException e)
		// {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

		// ParametrizedSolver solver1 = new ParametrizedSolver();
		// Object[] params = new Object[]
		// { "aaa", 100 };
		// solver.RunObjectMethods(solver1, params);
	}

}
